package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wenwan.manage.mapper.GiveGetMapper;
import com.wenwan.manage.pojo.Content;
import com.wenwan.manage.pojo.GiveGet;
import com.wenwan.manage.pojo.LimitPage;
import com.wenwan.manage.pojo.User;
import com.wenwan.manage.service.GiveGetService;
import com.wenwan.manage.service.GiveGetVolumeReadingService;
import com.wenwan.manage.service.UserService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class GiveGetServiceImpl implements GiveGetService {

	@Autowired
	private GiveGetMapper giveGetMapper;

	@Autowired
	public UserService userService;

	@Override
	public Map<String, Object> create(String title, String content, Boolean type, String userId, Double price) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (title != null && title.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "title" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("title" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(content, new TypeToken<List<Content>>() {
			}.getType());

		} catch (Exception e) {
			// TODO: handle exception
			dto.put("data", false);
			dto.put("msg", "content" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("content" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		User user = (User) userService.selectOneById(userId).get("data");
		if (user == null) {
			dto.put("data", false);
			dto.put("msg", StatusSetting.MSG_10005);
			dto.put("code", StatusSetting.CODE_10005);
			// dto.setData(false);
			// dto.setCode(StatusSetting.CODE_10005);
			// dto.setMsg(StatusSetting.MSG_10005);
			return dto;
		} else {
			if (user.getReleaseNumber() == null || user.getReleaseNumber() <= 0) {
				dto.put("data", false);
				dto.put("msg", StatusSetting.MSG_10006);
				dto.put("code", StatusSetting.CODE_10006);
				// dto.setData(false);
				// dto.setCode(StatusSetting.CODE_10006);
				// dto.setMsg(StatusSetting.MSG_10006);
				return dto;
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", Util.UUID());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("title", title);
		map.put("content", content);
		map.put("type", type);
		map.put("userId", userId);
		map.put("price", price);

		Boolean data = giveGetMapper.create(map);
		if (!data) {
			// dto.setCode(StatusSetting.CODE_10000);
			// dto.setMsg(StatusSetting.MSG_10000);
			dto.put("data", false);
			dto.put("msg", StatusSetting.MSG_10000);
			dto.put("code", StatusSetting.CODE_10000);
			return dto;
		} else {

			userService.update(userId, null, null, null, null, null, -1, null);

		}
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Boolean data = giveGetMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize, String userId, Boolean isForExamine,
			Boolean isForManager, Boolean isForUser, Boolean isExamine, Boolean isAdopt) {
		// TODO Auto-generated method stub
		// DataToObject<List<GiveGet>> dto = new DataToObject<List<GiveGet>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		map.put("isForExamine", isForExamine);
		map.put("isForManager", isForManager);
		map.put("isForUser", isForUser);
		map.put("isExamine", isExamine);
		map.put("isAdopt", isAdopt);

		List<GiveGet> data = giveGetMapper.selectAll(map);
		LimitPage limitPage = new LimitPage();
		limitPage.setCurrentPage(currentPage);
		limitPage.setPageSize(pageSize);
		Integer maxSize = giveGetMapper.selectMaxSize(map);
		limitPage.setMaxSize(maxSize);

		// dto.setLimitPage(limitPage);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("limitPage", limitPage);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id, String userId) {
		// TODO Auto-generated method stub
		// DataToObject<GiveGet> dto = new DataToObject<GiveGet>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("userId", userId);

		GiveGet data = giveGetMapper.selectOneById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Autowired
	private GiveGetVolumeReadingService giveGetVolumeReadingService;

	@Override
	public Map<String, Object> selectOneByIdReading(String id, String userId) {
		// TODO Auto-generated method stub
		// 向供求读者表添加数据
		// DataToObject<GiveGet> dto = new DataToObject<GiveGet>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Boolean success = (Boolean) giveGetVolumeReadingService.isExist(DigestUtils.md5Hex(id + userId).toUpperCase())
				.get("data");

		// 如果已存在
		if (success != null && success) {

		} else {
			// +1
			this.update(id, null, null, null, null, null, 1, null, null, null);
			// 添加到数据库
			giveGetVolumeReadingService.create(id, userId);

		}

		// 查询结果
		dto = this.selectOneById(id, userId);
		// 查询推荐
		@SuppressWarnings("unchecked")
		List<GiveGet> cliData = (List<GiveGet>) this.selectRecommend(1, 2).get("data");
		// dto.setCliData(cliData);
		dto.put("cliData", cliData);
		return dto;
	}

	@Override
	public Map<String, Object> update(String id, String title, String content, Boolean type, String userId,
			Double price, Integer countReading, Integer countShare, Integer countWell, Boolean isUndercarriage) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (title != null && title.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "title" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("title" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(content, new TypeToken<List<Content>>() {
			}.getType());

		} catch (Exception e) {
			// TODO: handle exception
			dto.put("data", false);
			dto.put("msg", "content" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("content" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("title", title);
		map.put("content", content);
		map.put("type", type);
		map.put("userId", userId);
		map.put("price", price);
		map.put("countReading", countReading);
		map.put("countShare", countShare);
		map.put("countWell", countWell);
		map.put("updateTime", Util.getTime());
		map.put("isUndercarriage", isUndercarriage);
		Boolean data = giveGetMapper.update(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectRecommend(Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		// DataToObject<List<GiveGet>> dto = new DataToObject<List<GiveGet>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);

		List<GiveGet> data = giveGetMapper.selectRecommend(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
